import { defineConfig } from "vite"
import electron from "vite-electron-plugin"
import { rmSync } from "fs"
import pkg from "./package.json"
import path from 'path'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'

/** 清空dist */
rmSync("dist", { recursive: true, force: true })

/** 配置项文档：https://cn.vitejs.dev/config */
export default defineConfig({
    resolve: {
        alias: {
            '@': path.resolve(__dirname, 'src')
        }
    },
    server: {
        /** 是否自动打开浏览器 */
        open: false,
        /** 设置 host: true 才可以使用 Network 的形式，以 IP 访问项目 */
        host: pkg.env.host,
        /** 端口号 */
        port: pkg.env.port,
        proxy: {
            '/api': {
                target: "http://127.0.0.1:8086",
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, ""),
            }
        }
    },
    build: {
        /** 单个 chunk 文件的大小超过 2048KB 时发出警告 */
        chunkSizeWarningLimit: 2048,
        /** 禁用 gzip 压缩大小报告 */
        reportCompressedSize: false,
        rollupOptions: {
            output: {
                /**
                 * 分块策略
                 * 1. 注意这些包名必须存在，否则打包会报错
                 * 2. 如果你不想自定义 chunk 分割策略，可以直接移除这段配置
                 */
                // manualChunks: {
                //   vue: ["vue", "vue-router", "pinia"],
                //   element: ["element-plus", "@element-plus/icons-vue"],
                //   vxe: ["vxe-table", "vxe-table-plugin-element", "xe-utils"]
                // }
            }
        }
    },
    /** 混淆器 */
    esbuild: {
        /** 打包时移除 console.log */
        pure: ["console.log"],
        /** 打包时移除 debugger */
        drop: ["debugger"],
        /** 打包时移除所有注释 */
        legalComments: "none"
    },
    /** Vite 插件 */
    plugins: [
        vue(),
        AutoImport({
            imports: ["vue", 'vue-router'],
            dts: "src/auto-import.d.ts",
            resolvers: [AntDesignVueResolver()],
        }),
        Components({
            resolvers: [
                AntDesignVueResolver({
                    importStyle: false
                }),
            ],
        }),
        createSvgIconsPlugin({
            // 要缓存的图标文件夹
            iconDirs: [path.resolve(__dirname, 'src/icons')],
            // 执行 icon name 的格式
            symbolId: 'icon-[name]'
        }),
        electron({
            outDir: "dist",
            include: ["script"],
            transformOptions: {
                sourcemap: false
            }
        })
    ],
    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@use '@/styles/var.scss' as *;`
            }
        }
    },
    clearScreen: false
})
